<?php

#
# Brings in an action object to take multiple variables for each action
#
# Full object identifier:
#
# action
#	modAlarm | delAlarm | addAlarm | serAlarm
#
# [alarmName]
#	Takes the name of the Alarm. Can also be used as a search term
#
# [alarmHour]
#	Takes the hour field of the alarm. This can be used in searches but is
#	mainly for adding and modding alarms.
#
# [alarmMinues]
#	Much the same as alarmHour , this can be used with searches but is 
#	mainly for adding and modding alarms.
#
# [alarmId]
#	Used for existing alarms to modify and delete them.
#
# [alarmFile]
#	The sound the alarm will make when it is triggered.
#
# formResponseCode
#	An error code for the status at the end of the execution of this script
#
# formResponseText
#	A text based representation of the data from the execution process. This
#	should give a user friendly description of the problem.
#

if(isset($_POST['inputObject']))
{
	include 'alarm.php';
		
	$alarmObject = json_decode($_POST['inputObject']);
	$actionIdentifier = $alarmObject->action;
	$outputObject = new Alarm();
	
	
	if ($actionIdentifier == "delAlarm")
	{
		#code for alarm deletion
		if(crowFather('select count(*) from alarm where alarmID = "' . $alarmObject->alarmId . '" and alarmName = "' . $alarmObject->alarmName . '";',5) > 0)
		{
			#ID Exists
			crowFather("delete from alarm where alarmID = " . $alarmObject->alarmId . ";",3);
			
			if(crowFather('select count(*) from alarm where alarmID = "' . $alarmObject->alarmId . '";',5) == 0)
			{
				$outputObject->formResponseText = "Successfully deleted alarm \n\nAlarm ID:" . $alarmObject->alarmId . "\n\n Alarm Name: " . $alarmObject->alarmName;
				$outputObject->formResponseCode = 0;
			}
			else
			{
				$outputObject->formResponseText =  "The alarm failed to delete and still exists";
				$outputObject->formResponseCode = 1;
			}
			
		}
		else
		{
			$outputObject->formResponseText =   "The alarm ID or Name does not exist or the name does not match the Id. Please refresh the page and see if the alarm appears.";
			$outputObject->formResponseCode = 1;
		}
		
	}
	
	if ($actionIdentifier == "addAlarm")
	{
		#code for alarm addition	
	}
	
	if ($actionIdentifier == "modAlarm")
	{
		#code for modding alarm
	}
	$finalObject = array_merge((array) $alarmObject , (array)$outputObject);
	print json_encode($finalObject);
}
else
{
	return 'modifyAlarm.php is POST only';
}

?>